package cn.itsource.controller;

import cn.itsource.domain.Bill;
import cn.itsource.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Bill> billList = billService.findAll();
        model.addAttribute("bills", billList);
        return "bill/billList";
    }

    @RequestMapping("/delete")
    public String del(Integer id) {
        billService.delete(id);
        return "redirect:/bill/list";
    }

    @RequestMapping("/jump")
    public String jump(Integer id, Model model) {
        if (id != null) {
            Bill billServiceOne = billService.findOne(id);
            model.addAttribute("bill", billServiceOne);
            return "bill/billUpdate";
        }
        return "bill/billAdd";
    }

    @RequestMapping("/add")
    public String add(Bill bill) {
        billService.add(bill);
        return "redirect:/bill/list";
    }

    @RequestMapping("/update")
    public String update(Bill bill) {
        billService.update(bill);
        return "redirect:/bill/list";
    }


    @RequestMapping("/view")
    public String view(Integer id, Model model, String name, HttpSession session) {
        Bill billServiceOne = billService.findOne(id);
        session.setAttribute("billname",name);
        model.addAttribute("bill", billServiceOne);
        return "bill/billView";
    }
}
